#include <stdio.h>
#include <unistd.h>

#define N 32

int main(int argc, char const *argv[])
{
    pid_t pid = -1;
    int pipefd[2]={0};
    char buf[N]={0};
    if(pipe(pipefd) < 0){ //创建无名管道
        printf("pipe failed\n");
        return -1;
    }
    pid = fork(); //创建子进程
    if(pid > 0){
        while(1){
            write(pipefd[1],"hello world!",sizeof("hello world!")); //往无名管道写入数据
            sleep(2);
        }
    }else if(pid == 0){
        while(1){
            read(pipefd[0],buf,sizeof(buf)); //读取无名管道数据放入buf中
            printf("buf = %s\n",buf); //打印buf数据终端显示
            sleep(1);
        }
    }

    return 0;
}
